//
//  DFUserCollectionController.m
//  DFront
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFUserCollectionController.h"
#import "FMDatabase.h"
#import "DFUserCollectionModel.h"
#import "DFProductsProductDetailController.h"
#import "DFUserCollectionCell.h"
#import "FMDatabase.h"

@interface DFUserCollectionController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)FMDatabase *dataBase;

@end

@implementation DFUserCollectionController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.dataBaseArr = [NSMutableArray array];
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 40)];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.mainTableView registerClass:[DFUserCollectionCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:self.topView];
    [self.topView addSubview:self.backBtn];
    [self.view addSubview:self.mainTableView];
    
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES).firstObject stringByAppendingPathComponent:@"DFront.db"];
    FMDatabase *database = [FMDatabase databaseWithPath:filePath];
    if ([database open]) {
        NSLog(@"打开数据库成功");
    }else
    {
        NSLog(@"打开数据库失败");
    }
    FMResultSet *resultSet = [database executeQueryWithFormat:@"select * from collections"];
    while ([resultSet next]) {
        DFUserCollectionModel *model = [[DFUserCollectionModel alloc]init];
        model.name = [resultSet stringForColumn:@"name"];
        model.price = [resultSet stringForColumn:@"price"];
        model.proId = [resultSet stringForColumn:@"proId"];
        model.subcateId = [resultSet stringForColumn:@"subcateId"];
        [self.dataBaseArr addObject:model];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return self.dataBaseArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFUserCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLab.text =  [self.dataBaseArr[indexPath.row] name];
    cell.priceLab.text = [@"￥" stringByAppendingString:[self.dataBaseArr[indexPath.row] price]];
       return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFProductsProductDetailController *detailController = [[DFProductsProductDetailController alloc]init];
    detailController.subcateId = [self.dataBaseArr[indexPath.row] subcateId];
    detailController.proId = [self.dataBaseArr[indexPath.row] proId];
    [self.navigationController pushViewController:detailController animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//  先删除数据库元素，再删除数组元素
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"DFront.db"];
    _dataBase = [FMDatabase databaseWithPath:filePath];
    if ([_dataBase open]) {
        NSLog(@"打开成功");
    }
    [_dataBase executeUpdate:@"delete from collections where proId = ?",[self.dataBaseArr[indexPath.row] proId]];
    [self.dataBaseArr removeObjectAtIndex:indexPath.row];
    [self.mainTableView reloadData];;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
